package org.zhixiang.gisQuery;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.zhixiang.gisQuery.query.GPSLocator;
import org.zhixiang.gisQuery.query.RectangleMBR;

/**
 * 
 * @say little Boy, don't be sad.
 * @name Rezar
 * @time 2018年9月14日 下午3:44:13
 * @Desc 些年若许,不负芳华.
 *
 */
public class RandomLonLat {

	public static int count = 0;

	public static void main(String[] args) throws IOException, InterruptedException {
		final GPSLocator locator = new GPSLocator("/Users/rezar/Desktop/gisDatas");
		// final GPSLocator locator = new
		// GPSLocator("F:\\workspace\\gisQuery\\src\\main\\resources\\gisDatas");
		doTest(locator, randomPois(1000000), false);
	}

	/**
	 * @param locator
	 * @param randomPois
	 */
	private static void doTest(final GPSLocator locator, List<Double[]> randomPois, boolean output) {
		long useTime = 0;
		long time1 = System.currentTimeMillis();
		for (Double[] queryPoi : randomPois) {
			// RectangleMBR doQuery = locator.doQuery(queryPoi[0], queryPoi[1]);
			locator.doQuery(queryPoi[0], queryPoi[1]);
			// if (output) {
			// System.out.println(
			// queryPoi[1] + "," + queryPoi[0] + "\tmbr:" + (doQuery == null ?
			// "无结果" : doQuery.getCity()));
			// }
		}
		long time2 = System.currentTimeMillis();
		useTime = (time2 - time1);
		System.out.println("use all time:" + useTime);
		System.out.println("mbr check ,use all time:" + RectangleMBR.useTime + "\t" + GPSLocator.maxCheckCount);
	}

	/**
	 * @param i
	 * @return
	 */
	private static List<Double[]> randomPois(int count) {
		List<Double[]> retList = new ArrayList<>();
		for (int i = 0; i < count; i++) {
			String randomLonLat = randomLonLat(100.3710937500d, 116.2792968750d, 25.8789944002d, 38.8910328265d, 0);
			String[] split = randomLonLat.split(",");
			retList.add(new Double[] { Double.parseDouble(split[0]), Double.parseDouble(split[1]) });
		}
		System.out.println("success random pois over:" + retList.size());
		return retList;
	}

	/**
	 * 在矩形内随机生成经纬度
	 * 
	 * MinLon：最小经度 MaxLon：最大经度 MinLat：最小纬度 MaxLat：最大纬度 type：设置返回经度还是纬度
	 */
	public static String randomLonLat(double MinLon, double MaxLon, double MinLat, double MaxLat, int type) {
		BigDecimal db = new BigDecimal(Math.random() * (MaxLon - MinLon) + MinLon);
		String lon = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();// 小数后6位
		db = new BigDecimal(Math.random() * (MaxLat - MinLat) + MinLat);
		String lat = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
		if (type == 0) {
			return lon + "," + lat;
		} else if (type == 1) {
			return lon;
		} else {
			return lat;
		}
	}

}
